package com.bz.portal.service.impl;

import com.bz.pojo.TbItem;
import com.bz.pojo.TbItemDesc;
import com.bz.pojo.TbItemParamItem;
import com.bz.portal.feign.CommonItemFeignClient;
import com.bz.portal.feign.CommonRedisFeignClient;
import com.bz.portal.service.ItemService;
import com.bz.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    private CommonItemFeignClient commonItemFeignClient;
    @Autowired
    private CommonRedisFeignClient commonRedisFeignClient;

    /**
     * 查询商品基本信息
     * @param itemId
     * @return
     */
    @Override
    public Result selectItemInfo(Long itemId) {
        TbItem tbItem;
        try {
            tbItem = commonRedisFeignClient.selectItemBasicInfo(itemId);
            if (tbItem != null){
                return Result.ok(tbItem);
            }
        } catch (Exception e) {
            log.error("【查询缓存中的商品基本信息失败】,{}",e);
        }
        tbItem = commonItemFeignClient.selectItemInfo(itemId);
        if (tbItem != null){
            commonRedisFeignClient.insertItemBasicInfo(tbItem);
        }
        return Result.ok(tbItem);
    }

    /**
     * 查询商品描述
     * @param itemId
     * @return
     */
    @Override
    public Result selectItemDescByItemId(Long itemId) {
        TbItemDesc tbItemDesc;
        try {
            tbItemDesc = commonRedisFeignClient.selectItemDesc(itemId);
            if (tbItemDesc != null){
                return Result.ok(tbItemDesc);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        tbItemDesc = commonItemFeignClient.selectItemDescByItemId(itemId);
        if (tbItemDesc != null){
            commonRedisFeignClient.insertItemDesc(tbItemDesc);
        }
        return Result.ok(tbItemDesc);
    }

    /**
     * 根据商品ID查询商品规格参数
     * @param itemId
     * @return
     */
    @Override
    public Result selectTbItemParamItemByItemId(Long itemId) {
        TbItemParamItem tbItemParamItem;
        try {
            tbItemParamItem = commonRedisFeignClient.selectItemParamItem(itemId);
            if (tbItemParamItem != null){
                return Result.ok(tbItemParamItem);
            }
        } catch (Exception e) {
            log.error("【查询缓存中的商品规格参数失败】,{}",e);
        }
        tbItemParamItem = commonItemFeignClient.selectTbItemParamItemByItemId(itemId);
        if (tbItemParamItem != null){
            commonRedisFeignClient.insertItemParamItem(tbItemParamItem);
        }
        return Result.ok(tbItemParamItem);
    }
}
